<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/User.php';
require_once 'CMS/Language.php';

//Người tạo : Nguyễn Thanh Hải - Team 2
//Module 	: NewPage
//Chức năng	: Xem Các trang user tạo
 
class NewPageController extends BaseUserController{

	public function init(){
		parent::init();
		$tbSetting = new User_Model_Parameter();
		$temp = $tbSetting->selectSetting("config");
		$layout = unserialize($temp[0]["parameter_value"]);
		if($layout['config_template'] !="")
		{
			$this->_helper->layout()->setLayout($layout['config_template']);
			$_SESSION['config_template'] = $layout['config_template'];
		}
		else
		{
			$this->_helper->layout()->setLayout('layoutmain');
			$_SESSION['config_template'] = 'layoutmain';
		}
	}

    public function indexAction(){
    	if(isset($_GET['page']))
    	{
    		$language = new Language(APPLICATION_PATH . '/widgets/lastestnews');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
					
    		$tbCurrentPage = new Admin_Model_CurrentPage();
    		$page = $tbCurrentPage->selectPageByCode($_GET['page']);
    		if(count($page)>0&&($page[0]['default']==0))
    		{
    			$_SESSION['page_code'] = $page[0]['page_code'];
    			$title_lang=unserialize($page[0]['title']);
				if(isset($title_lang[$name['name']]))
				$page[0]['title']=$title_lang[$name['name']];
				else
				$page[0]['title']="------------------";
				$des_lang=unserialize($page[0]['description']);
				if(isset($des_lang[$name['name']]))
				$page[0]['description']=$des_lang[$name['name']];
				else
				$page[0]['description']="------------------";
    			$this->view->title =  $page[0]['title'];
    			$this->view->description =  $page[0]['description'];
				$tbSidebarModule = new User_Model_SidebarModule();
				$Modules = $tbSidebarModule->selectAllModule();
				foreach($Modules as $module)
				{
					$database_show_widget [] = $module["module_code"];
					$database_order_widget [$module["module_code"]] =  $module["sort_order"];
				}
				
				$tbDefaultbarModule = new User_Model_DefaultModuleToPage();
				$defaultModules = $tbDefaultbarModule->selectAllModule($page[0]['page_code']);
				if($defaultModules!=null) foreach($defaultModules as $dmodule)
				{
					$content_show_widget [] = $dmodule["module_code"];
					$content_order_widget [$dmodule["module_code"]] =  $dmodule["sort_order"];
				}
				
				$tbContentbarModule = new User_Model_ContentModuleToPage();
				// mac dinh homepage
				$ContentModules = $tbContentbarModule->selectAllModule($page[0]['page_code']);	
				if($ContentModules!=null) foreach($ContentModules as $cmodule)
				{
					$content_show_widget [] = $cmodule["module_code"];
					$content_order_widget [$cmodule["module_code"]] =  $cmodule["sort_order"];
				}	
				
				$front     = Zend_Controller_Front::getInstance();
				$widgets = $front->getPlugins();
			
				foreach($widgets as $widget)
				{
					if(count($Modules) > 0){
						if($widget->name !="" && in_array($widget->name, $database_show_widget))
						{
							$widget->enable = 1;
							$widget->order = $database_order_widget[$widget->name];
						}
					}
					if(count($content_show_widget) > 0){
						if($widget->name !="" && in_array($widget->name, $content_show_widget))
						{
								$widget->enable = 1;
								$widget->order = $content_order_widget[$widget->name];
						}	
					}	
				}
    		}
    		else
    		{
    			$this->_redirect('index');
    		}
			
    	}
    	else
    	{
    		$this->_redirect('index');
    	}
		//End--------Xử lý thoát tài khoản
	}
	
}